package com.gitee.ice1938.module.admin.app.manage.appownerinfo;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.jdbc.core.BaseRepository;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.QueryInfo;
import org.springframework.stereotype.Repository;

/**
 * AppOwnerInfoController
 * 
 * 
 * @author ice1938@qq.com 2019-11-29 14:17:00
 *
 */
@Repository
public class AppOwnerInfoRepository extends BaseRepository {

	@Resource
	private JdbcTemplate jdbcTemplate;

	private QueryInfo querySql(Map<String, Object> param, Pageable pageable, Order order) {
		String keyword = param.get("q_search_key") == null ? null : param.get("q_search_key").toString().trim();
		List<Object> params = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder("SELECT ");
		// 拥有者ID
		sql.append("aoi.owner_uuid,");
		// 拥有者/开发商名称
		sql.append("aoi.owner_name,");
		// 管理员ID
		sql.append("aoi.intendant_id");
		//
		sql.append(" FROM app_owner_info as aoi where aoi.intendant_id = ?");
		params.add(param.get("intendantId"));
		// 模糊查询模版
		if (keyword != null && !keyword.isEmpty()) {
			sql.append(" and (aoi.owner_uuid like ? ");
			params.add("%" + keyword + "%");
			sql.append(" or aoi.owner_name like ?)");
			params.add("%" + keyword + "%");
		}

		String sqlCount = builderCountSql(sql.toString());
		if (order != null) {
			sql.append(" ORDER BY ");
			sql.append(underscoreName(order.getProperty()));
			sql.append(" ");
			sql.append(order.getDirection());
		}
		if (pageable != null) {
			sql.append(" LIMIT ");
			sql.append(pageable.getPageNumber() * pageable.getPageSize());
			sql.append(", ");
			sql.append(pageable.getPageSize());
		}
		return new QueryInfo(sql.toString(), sqlCount, params);

	}

	public Page<AppOwnerInfoEntity> findData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param, pageable, order);

		List<AppOwnerInfoEntity> content = jdbcTemplate.query(sqlinfo.getSql(), sqlinfo.getParams().toArray(),
		        BeanPropertyRowMapper.newInstance(AppOwnerInfoEntity.class));
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<AppOwnerInfoEntity>(content, pageable, total);
		}
		return new PageImpl<AppOwnerInfoEntity>(content);
	}

	public Page<Map<String, Object>> queryData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param, pageable, order);

		List<Map<String, Object>> content = jdbcTemplate.queryForList(sqlinfo.getSql(), sqlinfo.getParams().toArray());
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<Map<String, Object>>(content, pageable, total);
		}
		return new PageImpl<Map<String, Object>>(content);
	}

	public int update(AppOwnerInfoEntity data, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_owner_info  SET  ");
		List<Object> params = new ArrayList<>();
		// 拥有者ID
		sql.append("owner_uuid = ?,");
		params.add(data.getOwnerUuid());
		// 拥有者/开发商名称
		sql.append("owner_name = ?,");
		params.add(data.getOwnerName());
		// 管理员ID
		sql.append("intendant_id = ?");
		params.add(data.getIntendantId());

		sql.append(" WHERE ");
		// 拥有者ID
		sql.append("owner_uuid = ?");
		params.add(data.getOwnerUuid());
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int insert(AppOwnerInfoEntity data, String userLoginName) {

		StringBuilder sql = new StringBuilder("INSERT INTO app_owner_info ( ");
		List<Object> params = new ArrayList<>();
		// 拥有者ID
		sql.append("owner_uuid ,");
		params.add(data.getOwnerUuid());
		// 拥有者/开发商名称
		sql.append("owner_name ,");
		params.add(data.getOwnerName());
		// 管理员ID
		sql.append("intendant_id");
		params.add(data.getIntendantId());

		sql.append(" ) VALUES ( ");
		sql.append("? ,");
		sql.append("? ,");
		sql.append("? ");
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int disable(String ownerMd5, Boolean disable, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_owner_info  SET  disable =?");
		List<Object> params = new ArrayList<>();
		params.add(disable);
		sql.append(" WHERE ");
		sql.append("owner_uuid = ?");
		params.add(ownerMd5);
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int delete(String ownerMd5) {
		StringBuilder sql = new StringBuilder(" DELETE FROM app_owner_info WHERE ");
		List<Object> params = new ArrayList<>();
		sql.append("owner_uuid = ?");
		params.add(ownerMd5);
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int delete(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("DELETE FROM app_owner_info WHERE ");
		List<Object> params = new ArrayList<>();
		sql.append("owner_uuid in (");
		for (int i = 0; i < ids.size(); i++) {
			String id = ids.get(i);
			if (i == 0) {
				sql.append("?");
			} else {
				sql.append(",?");
			}
			params.add(id);
		}
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int[] batchInsert(List<AppOwnerInfoEntity> data, String userLoginName) {
		StringBuilder sql = new StringBuilder("INSERT INTO app_owner_info ( ");
		List<Object> params = new ArrayList<>();
		// 拥有者/开发商名称
		sql.append("owner_name ,");
		// 管理员ID
		sql.append("intendant_id");

		sql.append(" ) VALUES ( ");
		sql.append("? ,");
		sql.append("? ");
		sql.append(")");
		return jdbcTemplate.batchUpdate(sql.toString(), new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				AppOwnerInfoEntity item = data.get(i);
				int index = 1;
				// ps.setString(index++, item.getOwnerName());
				// ps.setString(index++, item.getIntendantId());
				// ps.setInt && setTimestamp 需要手动调整

			}

			@Override
			public int getBatchSize() {
				return data.size();
			}
		});

	}

	public AppOwnerInfoEntity findOne(String id) {
		String sql = "select *  FROM app_owner_info as aoi where owner_uuid =? ";
		try {
			return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(AppOwnerInfoEntity.class), id);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}

	}
}
